/*
 * Copyright (C) 2012 Martin Řehánek
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package cz.mzk.funnylib.api.builders;

import cz.mzk.funnylib.dao.UserDAO;
import cz.mzk.funnylib.dao.db.UserDAOImpl;
import cz.mzk.funnylib.entities.User;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Martin Řehánek
 */
public class UsersBuilder extends AbstractBuilder {

    UserBuilder userBuilder = new UserBuilder();
    private UserDAO dao = new UserDAOImpl(getJdbcTemplate());

    public String buildUsers() {
//        List<User> users = new ArrayList<User>();
//        for (int i = 0; i < 5; i++) {
//            User mockUser = userBuilder.mockUser(i);
//            users.add(mockUser);
//        }
//        return buildUsers(users);
        
        return buildUsers(dao.getUsers());
    }

    private String buildUsers(List<User> users) {
        StringBuilder result = new StringBuilder();
        result.append("{\n");
        result.append("\"").append("users").append("\"").append(":");
        result.append("[\n");


        for (int i = 0; i < users.size(); i++) {
            User user = users.get(i);
            result.append(userBuilder.buildUser(user));
            if (i != users.size() - 1) {
                result.append(",\n");
            }
        }
        result.append("\n]");
        result.append("\n}");
        return result.toString();
    }
}
